package Actions;
/*
 * Author - SAMBA
 */

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;

import FileParser.FileParser;
import Model.Conference;
import Model.Manuscript;
import Model.Review;
import Model.Reviewer;
import TableModels.ReviewerTemplate;

public class SubmitReviews extends AbstractAction{

	JTable table;
	String name;
	Conference aConference;
	
	public SubmitReviews(JTable table,String name,Conference aConference) {
		this.table = table;
		this.name = name;		
		this.aConference = aConference;
		this.putValue(NAME, "Submit Review");
		this.putValue(MNEMONIC_KEY, KeyEvent.VK_S);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(table.getSelectedRow() != -1) {
		JFileChooser chooser = new JFileChooser();
	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
	        "docx files", "docx");
	    chooser.setFileFilter(filter);
	    int returnVal = chooser.showOpenDialog(null);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	    	int row = table.convertRowIndexToModel(table.getSelectedRow());
			
	    	String title = (String) ((ReviewerTemplate)table.getModel()).getValueAt(row, 0);
	    	String authorName = (String) ((ReviewerTemplate)table.getModel()).getValueAt(row, 1);
	    	ArrayList<Review> aReviewList = new ArrayList<Review>();
	        Manuscript a_paper = new Manuscript(authorName, title, null, null, null, 0, 0, null, aReviewList);
	    	FileParser a_fp = new FileParser();
	    	Reviewer aReviewer = new Reviewer(a_fp, name, aConference);
	    	String filePath = "/Papers/"+chooser.getSelectedFile().getName();
	    	String confirmation = aReviewer.submitReview(a_paper, filePath);	    			      
	    	if(confirmation.equals("Review submitted")) {
	    		copyFile(chooser.getSelectedFile());
	    		((ReviewerTemplate)table.getModel()).setValueAt(filePath, row, 2);  //update the table
	    		table.repaint();
	    	}
	    	JOptionPane.showMessageDialog(null, confirmation);
	    }
		
		}

		
	}
	
	
	/**
	 * copies a source file to the papers folder
	 * @param src the source file
	 */
	private void copyFile(File src) {
    	InputStream inStream = null;
		OutputStream outStream = null;

		try{
			String fileName = src.getName();
			System.out.println("fileName!!!!!!"+fileName);
		    File destFile =new File("src/Papers/"+fileName); //don't add a "/" in front
		    												 //of src, errors will
		    												 //ruin your life.
		 
		    	    inStream = new FileInputStream(src);
		    	    outStream = new FileOutputStream(destFile);
		    	    
		    	    byte[] buffer = new byte[1024];
		 
		    	    int length;
		    	    //copy the file content in bytes 
		    	    while ((length = inStream.read(buffer)) > 0){
		 
		    	    	outStream.write(buffer, 0, length);
		 
		    	    }
		 
		    	    inStream.close();
		    	    outStream.close();
		 
		    	}catch(IOException e){
		    		e.printStackTrace();
		    	}
		}
	
}
